package com.wswy.wzcx.widget.shape;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.graphics.drawable.shapes.Shape;

public class TriangleShape extends Shape {


  private Path path = new Path();

  private Path pathTmp = null;

  private int color;
  private float radius=0;
  private RectF rectF = new RectF();

  public TriangleShape() {
    this(Color.parseColor("#80ffffff"),0);
  }

  public TriangleShape(int color,float radius) {
    this.color = color;
    this.radius = radius;
  }

  @Override
  protected void onResize(float width, float height) {
    super.onResize(width, height);
    path.reset();

    rectF.set(0,0,width,height);

    if (radius > 0){
      path.addRoundRect(rectF,new float[]{radius,radius,0,0,0,0,0,0},Direction.CW);

      if (pathTmp == null){
        pathTmp = new Path();
      }

      pathTmp.reset();

      pathTmp.moveTo(width,0);
      pathTmp.lineTo(width, height);
      pathTmp.lineTo(0,height);
      pathTmp.lineTo(width,0);
      pathTmp.close();

    }else {
      path.moveTo(0,0);
      path.lineTo(0,getWidth());
      path.lineTo(getHeight(),0);
      path.lineTo(0,0);
      path.close();
    }

  }

  @Override
  public void draw(Canvas canvas, Paint paint) {
    paint.setColor(color);
    paint.setStyle(Style.FILL);

    paint.setXfermode(null);
    if (radius > 0){
      canvas.drawPath(path,paint);

      if (pathTmp != null){

        paint.setColor(Color.WHITE);
        canvas.drawPath(pathTmp,paint);
      }



    }else {
      canvas.drawPath(path,paint);
    }
  }


  public void setColor(int color){
    this.color = color;
  }

}
